package com.quanshu.exchange.ui.gui.message.chat.emoji;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.base.BaseFragment;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.subjects.BehaviorSubject;

import static com.quanshu.exchange.ui.gui.message.chat.emoji.EmojiconPagerView.EMOJI_COLUMN;

/**
 * Created by jye on 2017/12/12.
 */
public class EmojiFragment extends BaseFragment {

    @BindView(R.id.emoji_recyclerView)
    RecyclerView emojiRecyclerView;
    private SingleAdapter<EmojiBean> adapter;

    public BehaviorSubject<EmojiBean> getEmojiDeleteClick() {
        return emojiDeleteClick;
    }

    public BehaviorSubject<EmojiBean> getEmojiClick() {
        return emojiClick;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private BehaviorSubject<EmojiBean> emojiDeleteClick = BehaviorSubject.create();
    private BehaviorSubject<EmojiBean> emojiClick = BehaviorSubject.create();

    public static EmojiFragment newInstance(ArrayList<EmojiBean> emojiBeans) {
        EmojiFragment f = new EmojiFragment();
        Bundle args = new Bundle();
        args.putSerializable("EmojiBean", emojiBeans);
        f.setArguments(args);
        return f;
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.chat_emoji_view;
    }

    @Override
    public void initView() {
        emojiRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), EMOJI_COLUMN));
        adapter = new SingleAdapter<>(new DataDisplayer<EmojiBean>(getActivity(), R.layout.item_row_emoji) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<EmojiBean> wrapper, EmojiBean item, int position) {
                if (item.getEmojiText() != null && item.getEmojiText().equals(EmojiUtil.DELETE_KEY)) {
                    holder.setBackground(R.id.iv_emoji, R.drawable.delete_expression);
                    holder.setText(R.id.iv_emoji, "");
                    holder.setOnClickListener(R.id.iv_emoji, view -> emojiDeleteClick.onNext(item));
                } else {
                    holder.setBackground(R.id.iv_emoji, -1);
                    holder.setText(R.id.iv_emoji, item.getCharX());
                    holder.setOnClickListener(R.id.iv_emoji, view -> emojiClick.onNext(item));
                }
            }
        });
        emojiRecyclerView.setAdapter(adapter);
    }

    private void setViewInfo() {
        List<EmojiBean> emojiBeans = (List<EmojiBean>) getArguments().getSerializable("EmojiBean");
        if (emojiBeans != null && emojiBeans.size() > 0 && adapter != null) {
            adapter.setData(emojiBeans);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        setViewInfo();
    }
}
